/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hotel.jpa;

import entities.RoomType;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.transaction.UserTransaction;

/**
 *
 * @author Robin
 */
public class RoomTypeJpaCtrl implements IJpaCtrl<RoomType> {

    private final UserTransaction utx;
    private final EntityManagerFactory emf;

    public RoomTypeJpaCtrl(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }

    @Override
    public void create(RoomType t) {
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        t = em.merge(t);
        tx.commit();

        em.close();
    }

    @Override
    public void destroy(RoomType t) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void edit(RoomType t) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public RoomType findEntity(RoomType t) throws NonexistentEntityException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<RoomType> findEntities() {
        EntityManager em = emf.createEntityManager();

        String query = "select rt from RoomType rt";
        Query q = em.createQuery(query);
        List<RoomType> result = q.getResultList();
        if(result == null){
            result = new ArrayList<RoomType>();
        }
        
        em.close();
        
        return result;
    }

    @Override
    public EntityManager getEntityManager() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int getEntityCount() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<RoomType> findAvailableRooms(int roomNb) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
